﻿using System;
using System.Text;
using Syncfusion.Pdf;
using Syncfusion.Windows.PdfViewer;

namespace PdfSdk.PdfViewer
{
    public class PdfiumViewer
    {
        private static readonly Type s_originalType;
        public static readonly string LockString;

        static PdfiumViewer()
        {
            s_originalType = typeof(PdfDocumentView).Assembly.GetType("Syncfusion.Windows.PdfViewer.PdfiumViewer");
            LockString = ObjectWrapper.GetFieldValue(s_originalType, "LockString") as string;
        }

        public static string ReferencePath
        {
            get => ObjectWrapper.GetPropertyValue(s_originalType) as string;
            set => ObjectWrapper.SetPropertyValue(s_originalType, value);
        }

        public static IntPtr FPDF_LoadDocument(string path, string password)
        {
            lock (LockString)
                return PdfiumNative.FPDF_LoadDocument(path, password);
        }

        public static int FPDF_GetPageCount(IntPtr document)
        {
            lock (LockString)
                return PdfiumNative.FPDF_GetPageCount(document);
        }

        public static int FPDF_GetPageSizeByIndex(
          IntPtr document,
          int page_index,
          out double width,
          out double height)
        {
            lock (LockString)
                return PdfiumNative.FPDF_GetPageSizeByIndex(document, page_index, out width, out height);
        }

        public static IntPtr FPDF_LoadPage(IntPtr document, int page_index)
        {
            lock (LockString)
                return PdfiumNative.FPDF_LoadPage(document, page_index);
        }

        public static void FPDF_ClosePage(IntPtr page)
        {
            lock (LockString)
                PdfiumNative.FPDF_ClosePage(page);
        }

        public static IntPtr FPDFBitmap_CreateEx(
          int width,
          int height,
          int format,
          IntPtr first_scan,
          int stride)
        {
            lock (LockString)
                return PdfiumNative.FPDFBitmap_CreateEx(width, height, format, first_scan, stride);
        }

        public static IntPtr FPDFBitmap_Create(int width, int height, int alpha)
        {
            lock (LockString)
                return PdfiumNative.FPDFBitmap_Create(width, height, alpha);
        }

        public static void FPDFBitmap_FillRect(
          IntPtr bitmapHandle,
          int left,
          int top,
          int width,
          int height,
          uint color)
        {
            lock (LockString)
                PdfiumNative.FPDFBitmap_FillRect(bitmapHandle, left, top, width, height, color);
        }

        public static void FPDF_RenderPageBitmap(
          IntPtr bitmapHandle,
          IntPtr page,
          int start_x,
          int start_y,
          int size_x,
          int size_y,
          int rotate,
          PdfiumNative.FPDF flags)
        {
            lock (LockString)
                PdfiumNative.FPDF_RenderPageBitmap(bitmapHandle, page, start_x, start_y, size_x, size_y, rotate, flags);
        }

        public static IntPtr FPDFBitmap_Destroy(IntPtr bitmapHandle)
        {
            lock (LockString)
                return PdfiumNative.FPDFBitmap_Destroy(bitmapHandle);
        }

        public static IntPtr FPDF_LoadMemDocument(byte[] data_buf, int size, string password)
        {
            lock (LockString)
                return PdfiumNative.FPDF_LoadMemDocument(data_buf, size, password);
        }

        public static IntPtr FPDFBitmap_GetBuffer(IntPtr bitmap)
        {
            lock (LockString)
                return PdfiumNative.FPDFBitmap_GetBuffer(bitmap);
        }

        public static void FPDF_CloseDocument(IntPtr bitmapHandle)
        {
            lock (LockString)
                PdfiumNative.FPDF_CloseDocument(bitmapHandle);
        }

        public static int FPDFBitmap_GetStride(IntPtr bitmap)
        {
            lock (LockString)
                return PdfiumNative.FPDFBitmap_GetStride(bitmap);
        }

        public static double FPDF_GetPageHeight(IntPtr page)
        {
            lock (LockString)
                return PdfiumNative.FPDF_GetPageHeight(page);
        }

        internal static void FPDF_RenderPage(
          IntPtr dc,
          IntPtr pages,
          int v1,
          int v2,
          int width,
          int height,
          int rotate,
          PdfiumNative.FPDF fPDF)
        {
            PdfiumNative.FPDF_RenderPage(dc, pages, v1, v2, width, height, rotate, fPDF);
        }

        public static double FPDF_GetPageWidth(IntPtr page)
        {
            lock (LockString)
                return PdfiumNative.FPDF_GetPageWidth(page);
        }

        public static int FPDFPage_GetRotation(IntPtr page)
        {
            lock (LockString)
                return PdfiumNative.FPDFPage_GetRotation(page);
        }

        public static uint FPDF_GetDocPermissions(IntPtr document)
        {
            lock (LockString)
                return PdfiumNative.FPDF_GetDocPermissions(document);
        }

        public static int FPDFText_GetText(IntPtr page, int start_index, int count, byte[] result)
        {
            lock (LockString)
                return PdfiumNative.FPDFText_GetText(page, start_index, count, result);
        }

        public static int FPDFText_CountChars(IntPtr page)
        {
            lock (LockString)
                return PdfiumNative.FPDFText_CountChars(page);
        }

        public static void FPDFText_GetCharBox(
          IntPtr page,
          int index,
          out double left,
          out double right,
          out double bottom,
          out double top)
        {
            lock (LockString)
                PdfiumNative.FPDFText_GetCharBox(page, index, out left, out right, out bottom, out top);
        }

        public static IntPtr FPDFText_LoadPage(IntPtr page)
        {
            lock (LockString)
                return PdfiumNative.FPDFText_LoadPage(page);
        }

        internal static IntPtr FPDFText_ClosePage(IntPtr page)
        {
            lock (LockString)
                return PdfiumNative.FPDFText_ClosePage(page);
        }

        internal static int FPDFText_GetCharIndexAtPos(
          IntPtr text_page,
          double x,
          double y,
          double xTolerance,
          double yTolerance)
        {
            lock (LockString)
                return PdfiumNative.FPDFText_GetCharIndexAtPos(text_page, x, y, xTolerance, yTolerance);
        }

        public static IntPtr FPDFLink_GetLinkAtPoint(IntPtr page, double x, double y)
        {
            lock (LockString)
                return PdfiumNative.FPDFLink_GetLinkAtPoint(page, x, y);
        }

        public static IntPtr FPDFLink_GetAction(IntPtr link)
        {
            lock (LockString)
                return PdfiumNative.FPDFLink_GetAction(link);
        }

        public static string FPDFAction_GetURIPath(IntPtr document, IntPtr action)
        {
            lock (LockString)
            {
                int num = 0;
                byte[] uri = null;
                num = PdfiumNative.FPDFAction_GetURIPath(document, action, uri, 0);
                if (num == 0)
                {
                    return "";
                }

                uri = new byte[num];
                num = PdfiumNative.FPDFAction_GetURIPath(document, action, uri, num);
                return Encoding.ASCII.GetString(uri).Trim(default(char));
            }
        }

        public static void FPDF_DeviceToPage(IntPtr page, int start_x, int start_y, int size_x, int size_y, PdfPageRotateAngle rotate, int device_x, int device_y, out double page_x, out double page_y)
        {
            lock (LockString)
                PdfiumNative.FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, (int)rotate, device_x, device_y, out page_x, out page_y);
        }
    }
}
